home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 July: SSW & SDK / Dev.CD Jul 98 SSW-SDK.toast / Development Kits / ColorSync 2.5 SDK / Goodies / ProfileRenamer / DSUserProcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-22  |  13.5 KB  |  456 lines  |  [TEXT/CWIE]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSUserProcs.c
  5. **
  6. **   Description:    Specific AppleEvent handlers used by the DropBox
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **
  18. *******************************************************************************
  19. **                      R E V I S I O N   H I S T O R Y
  20. *******************************************************************************
  21. **
  22. **      Date        Time    Author    Description
  23. **    --------    -----    ------    ---------------------------------------------
  24. **    06/23/94            LDR        Added support for ProcessItem and ProcessFolder handling
  25. **    02/20/94            LDR        Modified Preflight & Postflight to take item count
  26. **    01/25/92            LDR        Removed the use of const on the userDataHandle
  27. **    12/09/91            LDR        Added the new SelectFile userProc
  28. **                                Added the new Install & DisposeUserGlobals procs
  29. **                                Modified PostFlight to only autoquit on odoc, not pdoc
  30. **    11/24/91            LDR        Added the userProcs for pdoc handler
  31. **                                Cleaned up the placement of braces
  32. **                                Added the passing of a userDataHandle
  33. **    10/29/91            SCS        Changes for THINK C 5
  34. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  35. **                                Added a bunch of comments for clarification
  36. **    10/06/91    00:02    MTC        Converted to MPW C
  37. **    04/09/91    00:02    LDR        Added to Projector
  38. **
  39. ******************************************************************************/
  40.  
  41. #include <StandardFile.h>
  42. #include <CMApplication.h>
  43.  
  44. #include "DSUserProcs.h"
  45.  
  46. // Static Prototypes
  47. static OSErr ProcessFolder(FSSpecPtr myFSSPtr);
  48. static CMError    SetScriptProfileDescription ( CMProfileRef prof,
  49.                                               Str255 name, ScriptCode code);
  50. static char* Pstrcpy2C (char *dst, ConstStr255Param src, const unsigned char dstLen );
  51.  
  52.  
  53. /*
  54.     This routine is called during init time.
  55.     
  56.     It allows you to install more AEVT Handlers beyond the standard four
  57. */
  58. #pragma segment Main
  59. pascal void InstallOtherEvents (void) {
  60. }
  61.  
  62.  
  63. /*    
  64.     This routine is called when an OAPP event is received.
  65.     
  66.     Currently, all it does is set the gOApped flag, so you know that
  67.     you were called initally with no docs, and therefore you shouldn't 
  68.     quit when done processing any following odocs.
  69. */
  70. #pragma segment Main
  71. pascal void OpenApp (void) {
  72.     gOApped = true;
  73. }
  74.  
  75.  
  76. /*    
  77.     This routine is called when an QUIT event is received.
  78.     
  79.     We simply set the global done flag so that the main event loop can
  80.     gracefully exit.  We DO NOT call ExitToShell for two reasons:
  81.     1) It is a pretty ugly thing to do, but more importantly
  82.     2) The Apple event manager will get REAL upset!
  83. */
  84. #pragma segment Main
  85. pascal void QuitApp (void) {
  86.     gDone = true;    /*    All Done! */
  87. }
  88.  
  89.  
  90. /*    
  91.     This routine is the first one called when an ODOC or PDOC event is received.
  92.     
  93.     In this routine you would place code used to setup structures, etc. 
  94.     which would be used in a 'for all docs' situation (like "Archive all
  95.     dropped files")
  96.  
  97.     Obviously, the opening boolean tells you whether you should be opening
  98.     or printing these files based on the type of event recieved.
  99.     
  100.     NEW IN 2.0!
  101.     The itemCount parameter is simply the number of items that were dropped on
  102.     the application and that you will be processing.  This gives you the ability
  103.     to do a single preflight for memory allocation needs, rather than doing it
  104.     once for each item as in previous versions.
  105.     
  106.     userDataHandle is a handle that you can create & use to store your own
  107.     data structs.  This dataHandle will be passed around to the other 
  108.     odoc/pdoc routines so that you can get at your data without using
  109.     globals - just like the new StandardFile.  
  110.     
  111.     We also return a boolean to tell the caller if you support this type
  112.     of event.  By default, our dropboxes don't support the pdoc, so when
  113.     opening is FALSE, we return FALSE to let the caller send back the
  114.     proper error code to the AEManager.
  115.  
  116.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  117. */
  118. #pragma segment Main
  119. pascal Boolean PreFlightDocs (Boolean opening, short itemCount, Handle *userDataHandle) {
  120. #pragma unused ( itemCount )
  121. #pragma unused ( userDataHandle )
  122.  
  123.     return opening;        // we support opening, but not printing - see above
  124. }
  125.  
  126.  
  127. /*    
  128.     This routine is called for each file passed in the ODOC event.
  129.     
  130.     In this routine you would place code for processing each file/folder/disk that
  131.     was dropped on top of you.
  132.     
  133.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  134. */
  135. #pragma segment Main
  136. pascal void OpenDoc ( FSSpecPtr myFSSPtr, Boolean opening, Handle userDataHandle ) {
  137. #pragma unused ( opening )
  138. #pragma unused ( userDataHandle )
  139.     OSErr    err = noErr;
  140.     
  141.     
  142.     #ifdef qWalkFolders
  143.     /*
  144.         For this case we need to determine if the FSSpec is a file or folder.
  145.         If it's a folder, we then need to process each item in that folder,
  146.         otherwise just process the item.
  147.     */
  148.     if (FSpIsFolder(myFSSPtr))
  149.         err = ProcessFolder(myFSSPtr);
  150.     else
  151.         err = ProcessItem(myFSSPtr);
  152.     #else
  153.     /*
  154.         For this case we just call ProcessItem on the FSSpec above.
  155.     */
  156.     err = ProcessItem(myFSSPtr);
  157.     #endif
  158.     
  159.     // you should probably do something if you get back an error ;)
  160. }
  161.  
  162.  
  163. /*    
  164.     This routine is the last routine called as part of an ODOC event.
  165.     
  166.     In this routine you would place code to process any structures, etc. 
  167.     that you setup in the PreflightDocs routine.
  168.  
  169.     NEW IN 2.0!
  170.     The itemCount parameter was the number of items that you processed.
  171.     It is passed here just in case you need it ;)  
  172.     
  173.     If you created a userDataHandle in the PreFlightDocs routines, this is
  174.     the place to dispose of it since the Shell will NOT do it for you!
  175.     
  176.     You will probably want to remove the #pragma unusued (currently there to fool the compiler!)
  177. */
  178. #pragma segment Main
  179. pascal void PostFlightDocs ( Boolean opening, short itemCount, Handle userDataHandle ) {
  180. #pragma unused ( itemCount )
  181. #pragma unused ( userDataHandle )
  182.  
  183.     if ( (opening) && (!gOApped) )
  184.         gDone = true;    //    close everything up!
  185.  
  186.     /*
  187.         The reason we do not auto quit is based on a recommendation in the
  188.         Apple event Registry which specifically states that you should NOT
  189.         quit on a 'pdoc' as the Finder will send you a 'quit' when it is 
  190.         ready for you to do so.
  191.     */
  192. }
  193.  
  194.  
  195. /*
  196.     This routine gets called for each item (which could be either a file or a folder)
  197.     that the caller wants dropped.  The determining factor is the definition of the 
  198.     qWalkFolder compiler directive.   Either way, the item in question should be
  199.     processed as a single item and not "dissected" into component units (like subfiles
  200.     of a folder!)
  201. */
  202. OSErr ProcessItem(FSSpecPtr myFSSPtr)
  203. {
  204.     OSErr                err = noErr;
  205.     CMProfileLocation    profLoc;
  206.     CMProfileRef        prof = nil;
  207.     
  208.     profLoc.locType = cmFileBasedProfile;
  209.     profLoc.u.fileLoc.spec = *myFSSPtr;
  210.     
  211.     if (!err) err = CMOpenProfile( &prof, &profLoc );
  212.     
  213.     if (!err) err = SetScriptProfileDescription( prof, profLoc.u.fileLoc.spec.name, 0);
  214.     
  215.     if (!err) err = CMUpdateProfile( prof );
  216.  
  217.     if (prof) err = CMCloseProfile( prof );
  218.     
  219.     return(err);
  220. }
  221.  
  222. /*
  223.     This routine gets called for any folder (or disk) that the caller wants 
  224.     processed as a set of component items, instead of as a single entity.
  225.     The determining factor is the definition of the qWalkFolder compiler directive.
  226. */
  227. static OSErr ProcessFolder(FSSpecPtr myFSSPtr)
  228. {
  229.     OSErr        err = noErr;
  230.     short        index, oldIndex, localIndex;
  231.     FSSpec        localFSSpec, curFSSpec;
  232.     CInfoPBRec    cipb;
  233.     Str255        fName, vFName;
  234.     long        dirID, origDirID;
  235.     Boolean        foundPosition;
  236.  
  237.      // copy the source locally to avoid recursion problems
  238.      BlockMoveData(myFSSPtr, &localFSSpec, sizeof(FSSpec));
  239.      
  240.     //    get the dirID for THIS folder, not it's parent!
  241.     BlockMoveData(localFSSpec.name, fName, 32);
  242.     
  243.     cipb.hFileInfo.ioCompletion    = 0L;
  244.     cipb.hFileInfo.ioNamePtr    = fName;
  245.     cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  246.     cipb.hFileInfo.ioFDirIndex    = 0;    // use the dir & vRefNum;
  247.     cipb.hFileInfo.ioDirID        = localFSSpec.parID;
  248.     err = PBGetCatInfoSync(&cipb);
  249.     
  250.     if (!err)
  251.     {        
  252.         origDirID = cipb.dirInfo.ioDrDirID; // copy the sucker
  253.         index = 1;
  254.                 
  255.         // index through all contents of this folder
  256.         while (err == noErr)
  257.         {
  258.             dirID = origDirID;
  259.             localIndex = index;
  260.             fName [0] = 0;
  261.             cipb.hFileInfo.ioCompletion    = 0L;
  262.             cipb.hFileInfo.ioNamePtr    = fName;
  263.             cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  264.             cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  265.             cipb.hFileInfo.ioDirID        = dirID;
  266.             err = PBGetCatInfoSync(&cipb);
  267.  
  268.             if (!err)
  269.             {
  270.                 BlockMoveData(fName, curFSSpec.name, 32);
  271.                 curFSSpec.vRefNum    = cipb.hFileInfo.ioVRefNum;
  272.                 curFSSpec.parID        = dirID;
  273.             
  274.                 /*    
  275.                     Check to see if this entry is a folder.
  276.                 */
  277.                 if (cipb.hFileInfo.ioFlAttrib & ioDirMask)
  278.                     err = ProcessFolder(&curFSSpec);
  279.                 else
  280.                 {
  281.                     if (gTypeListCount == -1)
  282.                         err = ProcessItem(&curFSSpec);
  283.                     else
  284.                     {
  285.                         short i;
  286.                         for (i=0; i<gTypeListCount; i++)
  287.                             if (gTypeList[i] == cipb.hFileInfo.ioFlFndrInfo.fdType)
  288.                                 err = ProcessItem(&curFSSpec);
  289.                     }
  290.                 }    
  291.             
  292.                 /*    If we've had an error, get out! */
  293.                 if (err)    break;
  294.  
  295.                 // dirID = origDirID;    
  296.                 localIndex = index;    
  297.  
  298.                 /*    
  299.                     Now take into account new files being created
  300.                     in the current directory & messing up our index.
  301.                     See Dev.CD Vol. XI:Tools & Apps (Moof!):Misc Utilities:
  302.                     Disinfectant & Source 2.5.1:Sample:Notes:Scan Alg    
  303.                 */
  304.                 vFName [0] = 0;
  305.                 cipb.hFileInfo.ioCompletion    = 0L;
  306.                 cipb.hFileInfo.ioNamePtr    = vFName;
  307.                 cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  308.                 cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  309.                 cipb.hFileInfo.ioDirID        = dirID;
  310.                 err = PBGetCatInfoSync(&cipb);
  311.                 oldIndex = index;
  312.                 if (!err) {
  313.                     /*    If they're equal - same place, go to next */
  314.                     if (EqualString (vFName, fName, false, false))
  315.                         index++;
  316.                 }
  317.                 
  318.                 /*    If we didn't advance, then perhaps a file was created or deleted */
  319.                 if (oldIndex == index) {
  320.                     oldIndex        = index;    /* save off the old */
  321.                     index            = 0;        /* and start at the beginning */
  322.                     err                = noErr;
  323.                     vFName [0]        = 0;
  324.                     foundPosition    = false;
  325.                     
  326.                     while (!foundPosition) {
  327.                         index++;
  328.                         vFName [0] = 0;
  329.                         cipb.hFileInfo.ioCompletion    = 0L;
  330.                         cipb.hFileInfo.ioNamePtr    = vFName;
  331.                         cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  332.                         cipb.hFileInfo.ioFDirIndex    = index;    /* now use a real index */
  333.                         cipb.hFileInfo.ioDirID        = dirID;
  334.                         err = PBGetCatInfoSync(&cipb);
  335.                         
  336.                         if (err == fnfErr) {  // we've just been deleted
  337.                             index = oldIndex;
  338.                             foundPosition = true;
  339.                             err = noErr;    // have to remember to reset this!
  340.                         }
  341.                         
  342.                     /*    found same file & same index position */
  343.                     /*    so try the next item */
  344.                         if ((!foundPosition) && EqualString(fName, vFName, false, false)) {
  345.                             index++;
  346.                             foundPosition = true;
  347.                         }
  348.                     }
  349.                 }
  350.             }
  351.         }
  352.     }
  353.     
  354.     return(err);
  355. }
  356.  
  357. /*
  358.     This routine is called during the program's initialization and gives you
  359.     a chance to allocate or initialize any of your own globals that your
  360.     dropbox needs.
  361.     
  362.     You return a boolean value which determines if you were successful.
  363.     Returning false will cause DropShell to exit immediately.
  364. */
  365. pascal Boolean InitUserGlobals(void)
  366. {
  367.     return(true);    // nothing to do, we must be successful!
  368. }
  369.  
  370. /*
  371.     This routine is called during the program's cleanup and gives you
  372.     a chance to deallocate any of your own globals that you allocated 
  373.     in the above routine.
  374. */
  375. pascal void DisposeUserGlobals(void)
  376. {
  377.     // nothing to do for our sample dropbox
  378. }
  379.  
  380.  
  381.  
  382. /******************************************************************************/
  383. static CMError SetScriptProfileDescription ( CMProfileRef prof, Str255 name, ScriptCode code)
  384. {
  385.     CMError        err;
  386.     Ptr            tag;
  387.     UInt32        offset;
  388.     UInt32        nameLen;
  389.     
  390. //    4    OSType             typeDescriptor;        /* cmSigProfileDescriptionType */
  391. //    4    UInt32             reserved;            /* fill with 0x00 */
  392. //    4    UInt32             ASCIICount;            /* count of 1 byte characters */
  393. //    n+1    unsigned char     ASCIIName[2];        /* Variable size*/
  394. //    4    UInt32             UniCodeCode;
  395. //    4    UInt32             UniCodeCount;        /* count of 2 byte characters */
  396. //    0    unsigned char     UniCodeName[2];        /* Variable size */
  397. //    2    short             ScriptCodeCode;
  398. //    1    unsigned char     ScriptCodeCount;    /* count of 1 byte characters */
  399. //    n+1    unsigned char     ScriptCodeName[2];    /* Variable size */
  400.  
  401.     offset = 0;
  402.     nameLen = name[0]+1;
  403.     tag = NewPtrClear( 23 + 2*nameLen );
  404.     if (!tag) return memFullErr;
  405.     
  406.     // OSType  tag.typeDescriptor
  407.     *((OSType*)(tag+offset)) = cmSigProfileDescriptionType;
  408.     offset += 4;
  409.     
  410.     // UInt32  tag.reserved
  411.     offset += 4;
  412.  
  413.     // UInt32  tag.ASCIICount
  414.     *((UInt32*)(tag+offset)) = nameLen;
  415.     offset += 4;
  416.     
  417.     // tag.ASCIIName
  418.     Pstrcpy2C(tag+offset, name, 255 );
  419.     offset += nameLen;
  420.     
  421.     // tag.UniCodeCode, UniCodeCount, UniCodeName
  422.     offset += 8;
  423.     
  424.     // tag.ScriptCodeCode
  425.     *((SInt16*)(tag+offset)) = code;
  426.     offset += 2;
  427.     
  428.     // tag.ScriptCodeCount
  429.     *((UInt8*)(tag+offset)) = nameLen;
  430.     offset += 1;
  431.     
  432.     // tag.ScriptCodeName
  433.     Pstrcpy2C(tag+offset, name, 255 );
  434.     
  435.     err = CMSetProfileElement(prof, cmSigProfileDescriptionType, GetPtrSize(tag), tag);
  436.     
  437.     DisposePtr(tag);
  438.     return err;
  439. }
  440.  
  441.  
  442. static char* Pstrcpy2C (char *dst, ConstStr255Param src, const unsigned char dstLen )
  443. {
  444.     unsigned char bytesToCopy ;
  445.     
  446.     bytesToCopy = *src ;
  447.     if (bytesToCopy > dstLen)                    // make sure were not too big
  448.         bytesToCopy = dstLen ;
  449.         
  450.     BlockMoveData(    src + 1, dst,
  451.                     bytesToCopy ) ;                // copy string in
  452.     dst[bytesToCopy] = 0 ;                        // terminate c-string
  453.     
  454.     return dst ;
  455. }
  456.